<?php declare(strict_types=1);


namespace App\Rpc\Model\Dao;

use App\Rpc\Model\Entity\Organization as Entity;
use ReflectionException;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Db\Exception\DbException;

/**
 * Class OrganizationDao
 *
 * @Bean()
 *
 * @package App\Rpc\Model\Dao
 */
class OrganizationDao
{
    /**
     * @param array $data
     * @return int
     * @throws ReflectionException
     * @throws ContainerException
     * @throws DbException
     */
    public function insert(array $data): int
    {
        //这里开始新增消息
        $Model = Entity::new($data);
        $Model->save();
        return $Model->getId();
    }

    /**
     * @param $id
     * @param array $data
     * @return bool
     * @throws ContainerException
     * @throws DbException
     * @throws ReflectionException
     */
    public function update($id, array $data): bool
    {
        if(is_string($id)){
            $Model = Entity::where(['item_id' => $id])->first();
        }else if(is_int($id)){
            $Model = Entity::find($id);
        }else{
            $Model = NULL;
        }

        if ($Model == NULL) {
            return false;
        }

        $result = $Model->update($data);

        //更新某条消息
        return boolval($result);
    }

    /**
     * @param $id
     * @param bool $is_false
     * @return bool
     * @throws DbException
     */
    public function delete($id, $is_false = false): bool
    {
        if($is_false){
            //删除对应消息
            if(is_string($id)){
                //item_id
                $Model = Entity::where(['item_id' => $id])->first();
            }elseif(is_int($id)){
                //均可以进行删除
                $Model = Entity::find($id);
            }else{
                $Model = NULL;
            }
            if ($Model == NULL) {
                return false;
            }
            $result = $Model->delete($id);
            return boolval($result);
        }else{
            //伪删除
            return $this->update($id, ['frozen_state' => 1]);
        }
    }

    /**
     * @param int $id
     * @return array
     * @throws DbException
     */
    public function getById(int $id): array
    {
        $Model = Entity::find($id);

        if ($Model == NULL) {
            return [];
        }

        return $Model->toArray();
    }

    /**
     * @param array $where
     * @return array
     */
    public function getByWhere(array $where): array
    {
        $Model = Entity::where($where)->first();

        if($Model == NULL){
            return [];
        }

        return $Model->toArray();
    }

    /**
     * 获取消息列表
     * @param array $condition
     * @return array
     * @throws DbException
     */
    public function getList(array $condition): array
    {
        //首先是解析条件
        $p = 1;
        if (isset($condition['p']) && $condition['p'] > 0) {
            $p = $condition['p'];
        }
        $per = 10;
        if (isset($condition['per']) && $condition['per'] > 0) {
            $per = $condition['per'];
        }
        //解析where
        $where = [];
        if (isset($condition['where'])) {
            $where = $condition['where'];
        }

        $search = Entity::whereProp($where);

        //这里查询到总数
        $num = $search->count();

        $data = $search->forPage($p, $per)->get();

        $result['data'] = $data ?? [];
        $result['num'] = $num;

        return $result;
    }
}